//
//  FSCClassViewController.swift
//  FamiliesSchoolConnection
//
//  Created by 花花蔡 on 2016/12/27.
//  Copyright © 2016年 com.HuaHuaCai. All rights reserved.
//

import UIKit
import SVProgressHUD

class FSCClassViewController: UIViewController {

    let navi = FSCLeftViewNavi()
    var collectionV:UICollectionView!
    
    let labelW:CGFloat = screenWidth/8
    let labelH:CGFloat = 44
    
    var dataArr = [FSCClassTableModel]()

//MARK:视图生命周期
    override func viewDidLoad() {
        super.viewDidLoad()
        downLoadClassData()
        
        addNavi()
        setWeekView()
        setLeftClassTimeV()
        setCollectionView()
    }
    
//MARK:数据请求
    func downLoadClassData() -> Void {
        //获得接头地址
        let urlStr = "ZH-schoolname-S-curriculum"
        //设置参数
        let parm = ["key":userID]
        //请求数据
        FSCNetworkTools.shared.request(url: urlStr, parameters: parm) { [weak self](data, error) in
            
            //判断网络是否异常
            guard let data = data as? NSDictionary, let dictArr = data["data"] as? [NSDictionary] else {
                SVProgressHUD.showInfo(withStatus: "网络异常")
                return
            }
            
            //遍历数组
            for dict in dictArr {
                //获得当前字典中的所有key并排序
                let keys = (dict.allKeys as! [String]).sorted()
                //遍历key获得对应的字典
                for s in keys {
                    //根据遍历的key，获得对应的字典
                    let classDict = dict[s] as! [String:String]
                    let classModel = FSCClassTableModel(dict: classDict)
                    self?.dataArr.append(classModel)
                    
                    //处理午休数据
                    if s == "c4" {
                        let emptyModel = FSCClassTableModel(dict: [:])
                        self?.dataArr.append(emptyModel)
                    }
                }
            }
            self?.collectionV.reloadData()
        }
    
    
    
    }

    
    
}


//MARK: - UI相关

extension FSCClassViewController {
    
    //导航栏
    func addNavi() -> Void {
        navi.addNavigationInleftView(title: "课程表", VC: self)
        navi.btnBlock = { [weak self] in
            self?.dismiss(animated: true, completion: nil)
        }
        view.backgroundColor = UIColor.colorWithHex(hexColor: leftViewBgCol)
    }
    
    //设置周数组
    func setWeekView() -> Void {
        
        let weekTitles = ["","一","二","三","四","五","六","日"]
        //循环添加
        for (i,v) in weekTitles.enumerated() {
            let label = UILabel(title: v, color: UIColor.colorWithHex(hexColor: blueColor), fontSize: 15)
            label.frame = CGRect(x: CGFloat(i)*labelW, y: 64, width: labelW, height: labelH)
            view.addSubview(label)
        }
    }
    
    //设置左边课时视图
    func setLeftClassTimeV() -> Void {
        
        for item in 1..<10 {
            
            if item == 5 {
                let btn = UIButton(type: .custom)
                btn.frame = CGRect(x: 0, y: CGFloat(item)*labelH + 64, width: screenWidth, height: labelH)
            btn.setBackgroundImage(UIImage(named:"curriculum_wuxiu_bg"), for: .normal)
                btn.setTitle("午   休", for: .normal)
                view.addSubview(btn)
            }else {
            
                let label = UILabel(title: item>4 ? "\(item-1)":"\(item)", color: UIColor.colorWithHex(hexColor: blueColor), fontSize: 15)
                label.frame = CGRect(x: 0, y: CGFloat(item)*labelH + 64, width: labelW, height: labelH)
                view.addSubview(label)
            }
            //分割线
            let line = UIView(frame: CGRect(x: 5, y: CGFloat(item+1)*labelH + 64, width: screenWidth - 10, height: 1))
            line.backgroundColor = UIColor.colorWithHex(hexColor: blueColor)
            line.alpha = 0.5
            view.addSubview(line)
        }
    }
    
    //课表
    func setCollectionView() -> Void {
        //初始化
        let rect = CGRect(x: labelW, y: 64+labelH, width: screenWidth - labelW, height: 9 * labelH)
        //设置布局对象
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: labelW, height: labelH)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .horizontal
        
        collectionV = UICollectionView(frame: rect, collectionViewLayout: layout)
        collectionV.delegate = self
        collectionV.dataSource = self
        collectionV.register(FSCClassTitleCell.self, forCellWithReuseIdentifier: "classCell")
        collectionV.backgroundColor = UIColor.clear
        view.addSubview(collectionV)
    }
}


//MARK:集合视图的代理方法

extension FSCClassViewController:UICollectionViewDelegate,UICollectionViewDataSource {
    
    //格子个数

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "classCell", for: indexPath) as! FSCClassTitleCell
        let classModel = dataArr[indexPath.item]
        cell.model = classModel
        
        return cell
    }
}

